package com.osmino.contacts.search;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.osmino.contacts.R;

public class DublicateConstraintItem extends FrameLayout implements OnClickListener {

	protected TextView tv1, tv2;
	protected ISearchDublicateController mController;
	protected int mSection = 0;
	protected String mText;
	protected boolean mInSettingText;

	public DublicateConstraintItem(Context context, ISearchDublicateController controller) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.dublicate_constraint_item, this);
		mController = controller;
		mInSettingText = false;
		tv1 = (TextView) findViewById(R.id.merge_dublicate_head_tv_text);
		tv1.addTextChangedListener(new EditConstraintWatcher());
		tv2 = (TextView) findViewById(R.id.merge_dublicate_head_tv_merge);
		tv2.setOnClickListener(this);
	}

	public void setTextAndSection(String text, int section) {
		mInSettingText = true;
		if (!TextUtils.isEmpty(text)) {
			tv1.setText(text);
		}
		mSection = section;
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.merge_dublicate_head_tv_merge) {
			mController.onMergeDuplicateClick(mSection, tv1.getText().toString());
		}
	}

	private class EditConstraintWatcher implements TextWatcher {

		public EditConstraintWatcher() {//
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (!mInSettingText) {
				mController.onEditConstraint(mSection, tv1.getText().toString());
			}
			mInSettingText = false;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {//
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {//
		}

	}
}
